package com.ansion.utils;

import android.text.TextUtils;

import com.ansion.log.VLog;

/**
 * Created by wanghongjie on 2023/11/17
 * Describe : 校验url后缀是否为图片格式
 */
public class ImageUrlValidator {

    // 常见的图片格式后缀列表
    private static final String IMAGE_SUFFIX_JPG = ".jpg";
    private static final String IMAGE_SUFFIX_JPEG = ".jpeg";
    private static final String IMAGE_SUFFIX_PNG = ".png";
    private static final String IMAGE_SUFFIX_BMP = ".bmp";
    private static final String IMAGE_SUFFIX_WEBP = ".webp";

    public static boolean isImageUrl(String urlString) {
        if (TextUtils.isEmpty(urlString)) {
            if (VLog.OPEN_LOG)
                VLog.d("TAG_BOT_AVATAR", "ImageUrlValidator isImageUrl , urlString is empty , %s ", urlString);
            return false;
        }
        // 不是 http 开头的，如本地地址，直接返回true
        if (!urlString.startsWith("http")) {
            if (VLog.OPEN_LOG)
                VLog.d("TAG_BOT_AVATAR", "ImageUrlValidator isImageUrl , !urlString.startsWith(http) , %s ", urlString);
            return true;
        }
        return urlString.endsWith(IMAGE_SUFFIX_JPG)
                || urlString.endsWith(IMAGE_SUFFIX_JPEG)
                || urlString.endsWith(IMAGE_SUFFIX_PNG)
                || urlString.endsWith(IMAGE_SUFFIX_BMP)
                || urlString.endsWith(IMAGE_SUFFIX_WEBP);
    }
}
